package ThirdCommunication;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class Listener implements ActionListener, ListSelectionListener {
    //id框、pwd框
    private JTextField reg_idField;
    private JTextField reg_pwdField;
    private JTextField log_idField;//账号输入框
    private JTextField log_pwdField;//密码输入框
    private JTextArea chat_ALL_inputArea;//群聊用户输入框
    private JTextArea chat_One_inputArea;//私聊用户输入框
    private JTextArea chat_One_showArea;//私聊用户显示框
    private JList aliveList;//在线好友列表
    private ClientThread clientthread;
    private UI ui;
    private PersonalChatFrame personalFrame;//私聊ui
    private String myID;
    private String toID;//进行私聊时的聊天目标
    private JTextField file_ALL_jtf;//群聊文件选择框
    private JTextField file_One_jtf;//私聊文件选择框
    private String sendpath;//选择发送的文件路径
    private String savepath;//选择存储的文件路径
    private JFrame receiveFrame;//接收文件提示窗体
    private JFrame fileSaveFrame;//存储文件窗体
    private JTextField saveFilejtf;//存储文件选择框
    public Listener(ClientThread clientthread,UI ui) {
        this.clientthread = clientthread;
        this.ui = ui;
    }
    public Listener(ClientThread clientthread){
        this.clientthread = clientthread;
    }

    public void setReg_idField(JTextField reg_idField) {
        this.reg_idField = reg_idField;
    }

    public void setReg_pwdField(JTextField reg_pwdField) {
        this.reg_pwdField = reg_pwdField;
    }

    public void setLog_idField(JTextField log_idField) {
        this.log_idField = log_idField;
    }

    public void setLog_pwdField(JTextField log_pwdField) {
        this.log_pwdField = log_pwdField;
    }

    public void setChat_ALL_inputArea(JTextArea chat_ALL_inputArea) {
        this.chat_ALL_inputArea = chat_ALL_inputArea;
    }
    public void setChat_One_inputArea(JTextArea chat_One_inputArea) {
        this.chat_One_inputArea = chat_One_inputArea;
    }

    public void setChat_One_showArea(JTextArea chat_One_showArea) {
        this.chat_One_showArea = chat_One_showArea;
    }

    public void setAliveList(JList aliveList) {
        this.aliveList = aliveList;
    }
    public void setMyID(String myID){
        this.myID = myID;
    }

    public void setFile_ALL_Jtf(JTextField file_ALL_jtf) {this.file_ALL_jtf = file_ALL_jtf;}

    public void setFile_One_jtf(JTextField file_One_jtf) {this.file_One_jtf = file_One_jtf;}

    public void setPersonalFrame(PersonalChatFrame personalFrame) {
        this.personalFrame = personalFrame;
    }

    public void setReceiveFrame(JFrame receiveFrame) {
        this.receiveFrame = receiveFrame;
    }

    public void setFileSaveFrame(JFrame fileSaveFrame) {
        this.fileSaveFrame = fileSaveFrame;
    }

    public void setSaveFilejtf(JTextField saveFilejtf) {
        this.saveFilejtf = saveFilejtf;
    }

    public void setToID(String toID){this.toID = toID;}
    @Override
    public void actionPerformed(ActionEvent e) {
        switch (e.getActionCommand()){
            case "Login"://请求登录
                try {
                    regustLogin();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
                break;
            case "sendALL"://请求发送公屏消息
                try {
                    requestSendAll();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
                break;
            case "sendONE"://请求发送私聊信息
                try {
                    requestSendOne();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
                break;
            case "CLOSE"://请求退出私聊窗口
                requestClosePersonalFrame();
                break;
            case "quit"://请求退出
                try {
                    requestQuit();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
                break;
            case "fileALL"://请求展示发送群聊文件窗体
                ui.showFileUI();
                break;
            case "fileONE"://请求展示发送私聊文件窗体
                personalFrame.showFileUI();
                break;
            case "chooseFileALL"://请求选择群聊文件
                JFileChooser ALLjfc = new JFileChooser();//文件选择器
                if(ALLjfc.showOpenDialog(null)==ALLjfc.APPROVE_OPTION){
                    File ALLfile = ALLjfc.getSelectedFile();
                    sendpath = ALLfile.toString();
                    file_ALL_jtf.setText(sendpath);
                }
                break;
            case "chooseFileONE"://请求选择私聊文件
                JFileChooser ONEjfc = new JFileChooser();//文件选择器
                if(ONEjfc.showOpenDialog(null)==ONEjfc.APPROVE_OPTION){
                    File ONEfile = ONEjfc.getSelectedFile();
                    sendpath = ONEfile.toString();
                    file_One_jtf.setText(sendpath);
                }
                break;
            case "sendFileALL"://请求群聊发送文件
                try {
                    requestFile("0",sendpath);
                } catch (Exception ex) {

                    throw new RuntimeException(ex);
                }
                ui.closeFileUI();
                break;
            case "sendFileONE"://请求私聊发送文件
                try {
                    requestFile(toID,sendpath);

                } catch (Exception ex) {

                    throw new RuntimeException(ex);
                }
                personalFrame.closeFileUI();
                break;
            case "receiveFile"://接收发来的文件
                ui.showFileSaveUI();
                receiveFrame.dispose();
                break;
            case "refuseFile"://拒绝发来的文件
                receiveFrame.dispose();
                break;
            case "chooseSaveFile"://选择文件存储
                JFileChooser saveFilejfc = new JFileChooser();//文件选择器
                if(saveFilejfc.showOpenDialog(null)==saveFilejfc.APPROVE_OPTION){
                    File savefile = saveFilejfc.getSelectedFile();
                    savepath = savefile.toString();
                    saveFilejtf.setText(savepath);
                }
                break;
            case "saveFile"://存储文件
                try {

                    clientthread.writeFile(savepath,receiveFrame.getName(),myID);

                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
                fileSaveFrame.dispose();
                break;
            case "Register"://展示登录界面
                ui.showRegisterUI();
                break;
            case "Confirm"://请求注册
                try {
                    regustRegister();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
                break;
        }
    }

    //处理注册请求
    public void regustRegister()throws IOException{
        //获取输入框的账号密码
        String id = reg_idField.getText();
        String pwd = reg_pwdField.getText();
        clientthread.requestRegister(id,pwd);
    }

    //处理登录请求
    public void regustLogin()throws IOException{
        //获取输入框的账号密码
        String id = log_idField.getText();
        String pwd = log_pwdField.getText();
        clientthread.requestLogin(id,pwd);
    }
    //处理退出请求
    public void requestQuit() throws IOException {
        clientthread.requestQuit(myID);
        ui.close();//关闭程序
    }
    //请求发送公屏信息
    public void requestSendAll() throws IOException {
        String message = chat_ALL_inputArea.getText();
        clientthread.sendMessage("0",message);
        chat_ALL_inputArea.setText(null);//每发送完一条消息，清空发送框
    }
    //请求私发信息
    public void requestSendOne() throws IOException {
        String message = chat_One_inputArea.getText();
        clientthread.sendMessage(toID,message);
        chat_One_inputArea.setText(null);//每发送完一条消息，清空发送框
        //私聊双方的私聊窗体是两个不同的对象，不像群聊共用一个窗体对象
        chat_One_showArea.append(myID+"(yourself): "+message+"\r\n");
    }
    //请求关闭私聊窗口
    public void requestClosePersonalFrame(){
        clientthread.requestClosePersonalFrame(toID);
        personalFrame.close();
    }
    //请求发送文件

    public void requestFile(String targetID,String path) throws Exception {

        clientthread.sendFile(myID,targetID,path);
    }
    //请求
    @Override
    public void valueChanged(ListSelectionEvent e) {
        //鼠标点击会触发两次，按下一次松开一次，需要设置判断，只有在鼠标松开的时候执行
        if(!e.getValueIsAdjusting()){//按下返回true，松开返回false
            String targetID = (String)aliveList.getSelectedValue();
            /*int index = aliveList.getSelectedIndex();//选中的下标
            ListModel<String> lm = aliveList.getModel();//获取选择框模型
            String targetID = lm.getElementAt(index);//获取对应index的String
            System.out.println(targetID);*/
            if(targetID!=null){//有新用户上下线时需重置aliveList，可能会出现选取到空值的情况
                clientthread.requestPersonalFrame(myID,targetID);
            }
        }
    }

}
